package com.knight.mall.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.knight.mall.dto.PanicBuyProductDto;
import com.knight.mall.exception.ServiceException;
import com.knight.mall.mappers.PanicBuyingMapper;
import com.knight.mall.model.CollectPlace;
import com.knight.mall.model.Enterprise;
import com.knight.mall.model.PanicBuying;
import com.knight.mall.model.PanicBuyingProduct;
import com.knight.mall.model.Product;
import com.knight.mall.model.ProductSku;
import com.knight.mall.query.CollectPlaceQuery;
import com.knight.mall.query.PanicBuyingQuery;
import com.knight.mall.query.ProductQuery;
import com.knight.mall.service.CollectPlaceService;
import com.knight.mall.service.EnterpriseService;
import com.knight.mall.service.PanicBuyingProductService;
import com.knight.mall.service.PanicBuyingService;
import com.knight.mall.service.ProductService;
import com.knight.mall.service.ProductSkuService;

@Service(value = "panicBuyingService")
public class PanicBuyingServiceImpl implements PanicBuyingService {

	@Autowired
	private PanicBuyingMapper panicBuyingMapper;

	@Autowired
	private ProductService productService;

	@Autowired
	private EnterpriseService enterpriseService;

	@Autowired
	private CollectPlaceService collectPlaceService;

	@Autowired
	private ProductSkuService productSkuService;

	@Autowired
	private PanicBuyingProductService panicBuyingProductService;

	@Override
	public PanicBuying load(Long id) {
		return panicBuyingMapper.load(id);
	}

	@Override
	public Long add(PanicBuying t) throws ServiceException {
		panicBuyingMapper.insert(t);
		return 1L;
	}

	@Override
	public int update(PanicBuying t) {
		panicBuyingMapper.update(t);
		return 0;
	}

	@Override
	public int delete(Long id) {
		return panicBuyingMapper.delete(id);
	}

	@Override
	public List<PanicBuying> queryList(PanicBuyingQuery q) {
		return panicBuyingMapper.queryList(q);
	}

	@Override
	public int queryCount(PanicBuyingQuery q) {
		return panicBuyingMapper.queryCount(q);
	}

	/**
	 * 判断是否包含该地址
	 * 
	 * @param pb
	 * @param city
	 * @return
	 */
	public boolean isHasArea(PanicBuyingProduct pb, String city) {
		 String areas = pb.getSupportArea();
		if (city == null || "".equals(city.trim())) {
			return true;
		}

		if (areas.contains(",")) {
			String[] area = areas.split(",");

			for (String str : area) {
				if (str.equals(city)) {
					return true;
				}
			}
		} else {
			if (areas.equals(city)) {
				return true;
			}
		}
		return false;
	}

	public PanicBuyProductDto getPanicInfo(Long pdId,  Integer num, String city, Long skuId) {

		PanicBuyProductDto pdt = new PanicBuyProductDto();

		PanicBuyingProduct pbp = panicBuyingProductService.load(pdId);
		if (pbp != null) {
				pdt.setPanicBuyingProduct(pbp);
				if (isHasArea(pbp, city)) {
					pdt.setIsContaint("1");
				} else {
					pdt.setIsContaint("2");
				}

				//pdt.setPanicBuying(pb);

				pdt.setNum(num);
				
				if(pbp.getPostage()==null){
					pbp.setPostage(new BigDecimal("0"));
				}
				BigDecimal total = pbp.getPrice().multiply(new BigDecimal(num));
				total = total.add(pbp.getPostage());
				pdt.setTotal(total);
				Long pid = pbp.getProductId();
				ProductQuery pq = new ProductQuery();
				pq.setIdArray(pid);
				List<Product> listPS = productService.queryList(pq);

				if (listPS != null) {
					pdt.setProduct(listPS.get(0));
					Enterprise ese = enterpriseService.load(listPS.get(0).getEnterpriseId());
					if (ese != null) {
						pdt.setEnterprise(ese);
						if (ese.getDistributionMode().equals("2")) {
							CollectPlaceQuery cQuery = new CollectPlaceQuery();
							cQuery.setEnterpriseId(listPS.get(0).getEnterpriseId());
							cQuery.setClpStatus(1);
							List<CollectPlace> listCollects = collectPlaceService.queryList(cQuery);
							pdt.setListPlaces(listCollects);
						}
					}
				}

				if (skuId != null) {
					ProductSku productSku = productSkuService.load(skuId);
					pdt.setProductSku(productSku);
				}
		}
		return pdt;
	}

}
